class ConstantPool:
    def __init__(self):
        self.consts = []

    def readConstantPool(self, clsReader):
        from classfile.constant_info import readConstantInfo, CONSTANT_Long, CONSTANT_Double
        count = int.from_bytes(clsReader.readU16(), byteorder="big")
        self.consts.append(None)
        i = 1
        while i < count:
            tag, info = readConstantInfo(clsReader, self.consts)
            self.consts.append(info)
            i += 1
            if tag == CONSTANT_Long or tag == CONSTANT_Double:
                self.consts.append(None)
                i += 1
        return self.consts

    def getConstantInfo(self, index):
        return self.consts[index]

    def getNameAndType(self, index):
        ntInfo = self.getConstantInfo(index)
        name = self.getUTF8(ntInfo.nameIndex)
        _type = self.getUTF8(ntInfo.descIndex)
        return name, _type

    def getClassName(self, index):
        classInfo = self.getConstantInfo(index)
        return self.getUTF8(classInfo.nameIndex)

    def getUTF8(self, index):
        utf8Info = self.getConstantInfo(index)
        return utf8Info.getString()
